package com.airsystem.pos.bengkel.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import com.airsystem.pos.bengkel.controller.MasterCustomerController;
import com.airsystem.pos.bengkel.controller.MenuController;
import com.airsystem.pos.bengkel.util.TextUtil;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class MasterCustomer extends MasterCustomerController {

	public MasterCustomer(MenuController menuController) {
		super();

		this.menuController = menuController;
		initComponents();
		setup();
		load();
	}

	private void initComponents() {
		setTitle("Data Pelanggan");
		setFrameIcon(new ImageIcon(getClass().getResource("/images/customer.png")));
		setIconifiable(true);
		setClosable(true);

		baseLayout.columnWidths = new int[] { 600, 0 };
		baseLayout.rowHeights = new int[] { 0, 0 };
		baseLayout.columnWeights = new double[] { 1.0, 0.0 };
		baseLayout.rowWeights = new double[] { 0.0, 1.0 };
		getContentPane().setLayout(baseLayout);

		panelLayout.columnWidths = new int[] { 0, 250, 250 };
		panelLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		panelLayout.columnWeights = new double[] { 0.0, 0.0, 0.0 };
		panelLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
		basePanel.setLayout(panelLayout);

		namaLabel.setFont(new Font("Verdana", 0, 12));
		namaLabel.setText("Nama");

		kendaraanLabel.setFont(new Font("Verdana", 0, 12));
		kendaraanLabel.setText("Kendaraan");

		tahunLabel.setFont(new Font("Verdana", 0, 12));
		tahunLabel.setText("Tahun");

		nopolLabel.setFont(new Font("Verdana", 0, 12));
		nopolLabel.setText("No.Polisi");

		norangkaLabel.setFont(new Font("Verdana", 0, 12));
		norangkaLabel.setText("No.Rangka");

		nomesinLabel.setFont(new Font("Verdana", 0, 12));
		nomesinLabel.setText("No.Mesin");

		namaField.setFont(new Font("Verdana", 0, 12));
		namaField.setColumns(15);
		namaField.setDocument(new TextUtil(30, TextUtil.LETTER));

		merkkendaraanCombo.setFont(new Font("Verdana", 0, 12));
		merkkendaraanCombo.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent evt) {
				loadSubField(evt);
			}
		});

		submerkkendaraanCombo.setFont(new Font("Verdana", 0, 12));

		tahunField.setFont(new Font("Verdana", 0, 12));
		tahunField.setColumns(15);
		tahunField.setDocument(new TextUtil(4, TextUtil.NUMBER));

		nopolField.setFont(new Font("Verdana", 0, 12));
		nopolField.setColumns(15);
		nopolField.setDocument(new TextUtil(15, TextUtil.ALPHANUMERIC));

		norangkaField.setFont(new Font("Verdana", 0, 12));
		norangkaField.setColumns(15);
		norangkaField.setDocument(new TextUtil(15, TextUtil.ALPHANUMERIC));

		nomesinField.setFont(new Font("Verdana", 0, 12));
		nomesinField.setColumns(15);
		nomesinField.setDocument(new TextUtil(15, TextUtil.ALPHANUMERIC));

		idField.setFont(new Font("Verdana", 0, 12));
		idField.setColumns(5);
		idField.setVisible(false);

		fkField.setFont(new Font("Verdana", 0, 12));
		fkField.setColumns(5);
		fkField.setVisible(false);

		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));

		searchButton.setIcon(new ImageIcon(getClass().getResource("/images/search.png")));
		searchButton.setFont(new Font("Verdana", 1, 12));
		searchButton.setText("CARI");

		addButton.setIcon(new ImageIcon(getClass().getResource("/images/add.png")));
		addButton.setFont(new Font("Verdana", 1, 12));
		addButton.setText("TAMBAH");
		buttonPanel.add(addButton);

		saveButton.setIcon(new ImageIcon(getClass().getResource("/images/save.png")));
		saveButton.setFont(new Font("Verdana", 1, 12));
		saveButton.setText("SIMPAN");
		buttonPanel.add(saveButton);

		updateButton.setIcon(new ImageIcon(getClass().getResource("/images/update.png")));
		updateButton.setFont(new Font("Verdana", 1, 12));
		updateButton.setText("UBAH");
		buttonPanel.add(updateButton);

		deleteButton.setIcon(new ImageIcon(getClass().getResource("/images/delete.png")));
		deleteButton.setFont(new Font("Verdana", 1, 12));
		deleteButton.setText("HAPUS");
		buttonPanel.add(deleteButton);

		cancelButton.setIcon(new ImageIcon(getClass().getResource("/images/cancel.png")));
		cancelButton.setFont(new Font("Verdana", 1, 12));
		cancelButton.setText("BATAL");
		buttonPanel.add(cancelButton);

		initCustomers();

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				searchButtonActionPerformed(evt);
			}
		});

		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});

		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				saveButtonActionPerformed(evt);
			}
		});

		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				updateButtonActionPerformed(evt);
			}
		});

		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		basePanel.add(namaLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 0, 20, 0), 0, 0));
		basePanel.add(kendaraanLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 20, 0), 0, 0));
		basePanel.add(tahunLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 20, 0), 0, 0));
		basePanel.add(nopolLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 20, 0), 0, 0));
		basePanel.add(norangkaLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 20, 0), 0, 0));
		basePanel.add(nomesinLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 20, 0), 0, 0));
		basePanel.add(namaField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30, 20, 20, 0), 0, 0));
		basePanel.add(merkkendaraanCombo, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(submerkkendaraanCombo, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(tahunField, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(nopolField, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(norangkaField, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(nomesinField, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(searchButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 20, 20, 0), 0, 0));
		basePanel.add(buttonPanel, new GridBagConstraints(0, 7, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		basePanel.add(idField, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(fkField, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 20, 0), 0, 0));

		getContentPane().add(basePanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 600) / 2, (screenSize.height - 700) / 2, 600, 600);
	}

	private void initCustomers() {
		tablecustomerScroll.setViewportView(tableCustomer);
		tableCustomer.setFont(new Font("Verdana", 0, 12));
		tableCustomer.setModel(tableModel);

		tableCustomer.getTableHeader().setReorderingAllowed(false);
		tableCustomer.getColumnModel().getColumn(0).setPreferredWidth(200);
		tableCustomer.getColumnModel().getColumn(1).setPreferredWidth(250);
		tableCustomer.getColumnModel().getColumn(2).setPreferredWidth(100);
		tableCustomer.getColumnModel().getColumn(3).setPreferredWidth(150);
		tableCustomer.getColumnModel().getColumn(4).setPreferredWidth(150);
		tableCustomer.getColumnModel().getColumn(5).setPreferredWidth(150);

		/* Hide Column */
		tableCustomer.getColumnModel().getColumn(6).setMaxWidth(0);
		tableCustomer.getColumnModel().getColumn(6).setMinWidth(0);
		tableCustomer.getColumnModel().getColumn(6).setPreferredWidth(0);
		tableCustomer.getColumnModel().getColumn(7).setMaxWidth(0);
		tableCustomer.getColumnModel().getColumn(7).setMinWidth(0);
		tableCustomer.getColumnModel().getColumn(7).setPreferredWidth(0);

		tableCustomer.setPreferredScrollableViewportSize(new Dimension(150, 150));
		tableCustomer.setFillsViewportHeight(true);

		tableCustomer.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				tableCustomerMouseClicked(evt);
			}
		});

		getContentPane().add(tablecustomerScroll, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 1, 1, 1), 0, 0));
	}

	private void searchButtonActionPerformed(ActionEvent evt) {
		search();
	}

	private void tableCustomerMouseClicked(MouseEvent evt) {
		row = tableCustomer.getSelectedRow();

		if (evt.getClickCount() == 1 && tableCustomer.getRowCount() != 0) {
			init();
		}
	}

	private void addButtonActionPerformed(ActionEvent evt) {
		add();
	}

	private void saveButtonActionPerformed(ActionEvent evt) {
		save();
	}

	private void updateButtonActionPerformed(ActionEvent evt) {
		update();
	}

	private void deleteButtonActionPerformed(ActionEvent evt) {
		delete();
	}

	private void cancelButtonActionPerformed(ActionEvent evt) {
		cancel();
	}
}